package com.yanze.mvvmlearndemo.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;


import java.util.List;

/**
 * Created by MrZheng on 2018/3/12.
 */

public class ArticleAdapter<T> extends BaseAdapter {
    Context mContext;
    List<T> mdatas;
    LayoutInflater inflater;
    int layoutId;
    int vairiable;

    public ArticleAdapter(Context context, List<T> mdatas, int layoutId, int resId) {
        this.mContext = context;
        this.mdatas = mdatas;
        this.layoutId = layoutId;
        this.vairiable = resId;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return mdatas.size();
    }

    @Override
    public Object getItem(int i) {
        return mdatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup parent) {
        ViewDataBinding databinding;
        if (convertView == null) {
            databinding = DataBindingUtil.inflate(inflater, layoutId, parent, false);
        }else{
            databinding = DataBindingUtil.getBinding(convertView);
        }
        databinding.setVariable(vairiable, mdatas.get(i));
        return databinding.getRoot();
    }
}
